package com.ddf.publics.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.ddf.publics.config.AlipayConfig;
import com.ddf.publics.config.OrderCode;
import com.ddf.publics.entity.Fund;
import com.ddf.publics.entity.UserInfo;
import com.ddf.publics.service.AlipayService;
import com.ddf.publics.service.FundService;
import com.ddf.publics.service.UserInfoService;
import com.ddf.publics.tool.MoneyFund;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Override
    public void aliPay(HttpServletResponse response, HttpServletRequest request, Float money) throws IOException, AlipayApiException {
        response.setContentType("text/html;charset=utf-8");


        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl(AlipayConfig.notify_url);//支付结果异步通知

        //商户订单号，后台可以写一个工具类生成一个订单号，必填
        String order_number = OrderCode.getOrderCode();
        //付款金额，从前台获取，必填
        Float total_amount = money;

        //订单名称，必填
        String subject = "充值";
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                                    + "\"total_amount\":\"" + total_amount + "\","
                                    + "\"subject\":\"" + subject + "\","
                                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        AlipayTradePagePayResponse aliPayResponse = alipayClient.pageExecute(aliPayRequest);

        //输出
        out.println(aliPayResponse.getBody());
    }

    @Override
    public AlipayTradeQueryResponse selPayStatus(String orderNum) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{\"out_trade_no\":\""+orderNum+"\"}");
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        return response;
    }
}
